// pages/album_pic/album_pic.js
var app = getApp()
Page({
  data:{
    pic_count:0
  },
  onLoad:function(o){

    var album = wx.getStorageSync('album_info_s')

    this.setData({
      album:album,
      domain:app.globalData.domain,
      cover:album.Album_Pic_Cover_Id,
      up:app.globalData.System_Img_Url+'up.png',
      down:app.globalData.System_Img_Url+'down.png',
    })
    this.GetPics(album.Album_Id,1);
    // 页面初始化 options为页面跳转所带来的参数

  },


//封面
  Cover_Change(e){
    var cover = e.currentTarget.dataset.cover
    var coverid = e.currentTarget.dataset.coverid
    //console.log(coverid)
    var album = wx.getStorageSync('album_info_s')
   // album.Album_Pic_Cover = cover
   // album.Album_Pic_Cover_Id = coverid
  //  wx.setStorageSync('album_info_s',album)
    this.setData({
      cover:coverid,
      coveraddr:cover
    })
   // console.log(wx.getStorageSync('album_info_s'))
  //  wx.redirectTo({url:'../album_add/album_add'})

  },

//保存封面
  save_banner(){
    var album = wx.getStorageSync('album_info_s')
    album.Album_Pic_Cover = this.data.coveraddr
    album.Album_Pic_Cover_Id = this.data.cover
    wx.setStorageSync('album_info_s',album)
  //  console.log(wx.getStorageSync('album_info_s'))
    wx.redirectTo({url:'../album_add/album_add'})
  },


//图片上下移动
  Pic_Move(e){
    var that = this
    var t = new Date()

    
    if(!wx.getStorageSync('move_start') || t.getTime() - wx.getStorageSync('move_start') > 2000){
      var pid = e.currentTarget.dataset.pid
      var sort = e.currentTarget.dataset.sort
      var move = e.currentTarget.dataset.type
      var type = move=='up'?0:1
      wx.request({
        url:app.globalData.urls,
        data:{
          'GetTypes':'Picture_Move',
          aid:that.data.album.Album_Id,
          pid:pid,
          sort:sort,
          type:type
        },
        success:function(res){
          var st = that.GetPics(that.data.album.Album_Id,that.data.pic_info.Pnow)
        }
      })
        wx.setStorageSync('move_start',t.getTime())
    }else{
    //  console.log(22222)
    }

  },

//删除图片
  Pic_Del(e){
    var t = this
    var pid = e.currentTarget.dataset.pid
    var pnow = e.currentTarget.dataset.pnow
   // console.log(pid)
 //   console.log(pnow)
    wx.request({
      url:app.globalData.urls,
      data:{
        GetTypes:'Picture_Del',
        pid:pid
      },
      success:function(res){
      //  console.log(res.data)
        if(res.data == 'ok'){
          wx.showToast({
            title:'删除成功！'
          })
          t.GetPics(t.data.album.Album_Id,pnow)
        }else{
          wx.showToast({
            title:'删除失败！',
            icon:'loading'
          })
        }
      }
    })
  },

 
//上下翻页
  Flip_Page(e){
    var flip = e.currentTarget.dataset.type
    var pnow = e.currentTarget.dataset.pnow
    var albumid = e.currentTarget.dataset.albumid
    if(flip == 'last'){
      if(this.data.pic_info.Pnow > 1){
        var last = this.data.pic_info.Pnow -1
      }else{
        last = 1
      }
      this.GetPics(this.data.album.Album_Id,last)
    }else{
      if(this.data.pic_info.Pnow < this.data.pic_info.PageCount){
        var last = this.data.pic_info.Pnow + 1
      }else{
        var last = this.data.pic_info.PageCount
      }
      this.GetPics(this.data.album.Album_Id,last)
    }
  },


  GetPics(album_id,pnow=1,pcount=0){
    var t = this
    wx.request({
      url:app.globalData.urls,
      data:{
        'GetTypes':'Get_Pictures',
        'album_id':album_id,
        'pnow':pnow,
        'pcount':pcount
      },
      success:function(res){
     //   console.log(res.data)
        t.setData({
          pic_info:res.data
        })
        wx.setStorageSync('GetPics_stat',true)
      }
    })
  },



//添加图片
  UpFile(){
    var t = this
    wx.chooseImage({
      count:9,
      success: function(res) {
        wx.showLoading({
          title:'图片正在上传中...',
        })
        t.UpPic(t,res.tempFilePaths)
      }
    })
  },

  UpPic(t,path){
    if(t.data.pic_count < path.length){

   // console.log(t.data.pic_count +'=='+ path[t.data.pic_count])
   // console.log()

      wx.uploadFile({
        url: app.globalData.urls, 
        filePath:path[t.data.pic_count],
        name: 'file',
        formData:{
          'GetTypes':'Picture_Add',
          'album_id':t.data.album.Album_Id,
          'oid':wx.getStorageSync('openid')
        },
        complete: function(res){
        //  console.log(res.data)
          t.setData({
            pic_count:t.data.pic_count+1
          })
          if(res.data == 'ok'){
            t.UpPic(t,path)
          }
        }
      })
    }else{
      t.setData({
        pic_count:0
      })
      wx.showToast({
        title:'上传完成！'
      })
      this.GetPics(t.data.album.Album_Id,1);
    }
  }

})